<?php
class adm_role_model extends ci_model {


	public function __construct(){
		parent::__construct();
		$this->db = $this->load->database('default',true);
	}

	/**
	 *
	 *
		* @name get_role_level
		* @date 2013-1-9
		* @author eifel
		* @param user_id
		* @return 权限信息
		* @version 1.0
		* @description 根据用户id查询用户角色的等级
	 */
	public function get_role_level($user_id) {
		$user_id	=$this->db->escape($user_id);
		$sql		="select level from adm_user_role
						inner join adm_role on adm_role.role_id =adm_user_role.role_id 
						where adm_user_role.user_id =$user_id limit 1";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	public function query_user_menu($user_id) {
		$user_id	=$this->db->escape($user_id);
		$sql		="select adm_right.* from adm_user_role
						inner join adm_role on adm_role.role_id =adm_user_role.role_id
						inner join adm_role_right on adm_user_role.role_id =adm_role_right.role_id
						inner join adm_right on adm_role_right.right_id =adm_right.right_id
						where adm_user_role.user_id=$user_id
						and (adm_right.parent_id=0 or adm_right.url is not null)
						group by adm_right.right_id
						";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}

	public function query_admin_menu() {
		$sql		="select adm_right.* from adm_right
						where adm_right.parent_id=0 or url is not null";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}

	public function query_user_role($user_id) {
		$user_id	=$this->db->escape($user_id);
		$sql		="select level,adm_role.role_id,role_name,adm_role.parent_id from adm_user_role
						inner join adm_role on adm_role.role_id =adm_user_role.role_id 
						where adm_user_role.user_id=$user_id ";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}

	public function query_role_list($start,$limit) {
		$sql 	="select * from adm_role where role_id !='1'  order by role_id desc limit $start,$limit";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
	
	public function query_role_num(){
		$sql 	="select role_id from adm_role  ";
		$query	=$this->db->query($sql);
		return $query->num_rows();
	}
	public function query_role_by_name($role_name) {
		$role_name	=$this->db->escape($role_name);
		$sql		="select role_id from adm_role where role_name  =$role_name limit 1";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}
	public function add_role($role_data) {
		$data=array("role_name"		=>$role_data['role_name'],
					"description"	=>$role_data['description'],
					"parent_id"		=>$role_data['parent_id'],
					"created_by"	=>$role_data['created_by'],
					"created_time"	=>$role_data['created_time']);
		$this->db->insert("adm_role",$data);
		return $this->db->insert_id();
	}
	public function query_role_info($role_id) {
		$role_id 	=$this->db->escape($role_id);
		$sql		="select * from adm_role where role_id 	=$role_id limit 1";
		$query		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}
	
	public function modify_role($role_data,$role_id) {
		$data=array("role_name"		=>$role_data['role_name'],
					"description"	=>$role_data['description'],
					"modified_by"	=>$role_data['modified_by'],
					"modified_time"	=>$role_data['modified_time']);
		$this->db->where("role_id",$role_id);
		$this->db->update("adm_role",$data);
		return $this->db->insert_id();
	}
	public function delete_role($role_id) {
		$role_id	=$this->db->escape($role_id);
		$sql		="delete from adm_role where role_id =$role_id";
		$this->db->query($sql);
	}
	public function query_all_role() {
		$sql ="select role_id,role_name from adm_role where role_id !='1'  order by role_id desc";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
}